#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using Agresso.Interface.Layer.Service;
using System.Collections;
using System.IO;
using System.Configuration;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class UserControlSoftmarProfitCentreMapping : UserControl
    {
        User m_current_user;
        MainForm m_mainForm;
        string m_startDefault;
        Timer m_timerSearch = new Timer();
        ProfitCentreService m_profitCentreService;


        public UserControlSoftmarProfitCentreMapping(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            labelTitle.Text = name;
            this.Name = name;
            m_current_user = currentUser;
            m_profitCentreService = new ProfitCentreService();
            m_mainForm = mainForm;
            initColumnWidth();

            m_startDefault = ConfigurationManager.AppSettings["StartPath"];
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonExcel.Click += new EventHandler(toolBarButtonExcel_Click);
            listViewAlert.DoubleClick += new EventHandler(listViewAlert_DoubleClick);
            toolBarButtonRefresh_Click(null, null);
            initTimerSearch();
        }

        private void initColumnWidth()
        {
            listViewAlert.Columns[0].Width = 71;
            listViewAlert.Columns[1].Width = 69;
            listViewAlert.Columns[2].Width = 123;
            listViewAlert.Columns[3].Width = 213;
            listViewAlert.Columns[4].Width = 96;
            listViewAlert.Columns[5].Width = 32;
            listViewAlert.Columns[6].Width = 57;
            listViewAlert.Columns[7].Width = 83;
            listViewAlert.Columns[8].Width = 44;
            listViewAlert.Columns[9].Width = 100;
            listViewAlert.Columns[10].Width = 100;
            listViewAlert.Columns[10].Width = 100;
            listViewAlert.Columns[11].Width = 50;
        }

        void listViewAlert_DoubleClick(object sender, EventArgs e)
        {
            if (listViewAlert.SelectedItem == null) return;
            if (listViewAlert.SelectedItem.Tag == null) return;
            ProfitCentre a = (ProfitCentre)listViewAlert.SelectedItem.Tag;
            SoftmarProfitCentreMappingForm adf = new SoftmarProfitCentreMappingForm(a, this);
            adf.ShowDialog();
        }

        void toolBarButtonExcel_Click(object sender, EventArgs e)
        {
            if (listViewAlert.Items.Count == 0)
            {
                MessageBox.Show("No Data to export, please refresh first.");
                return;
            }
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("Table");
            foreach (ColumnHeader column in listViewAlert.Columns)
            {
                dt.Columns.Add(column.Text);
            }
            foreach (ListViewItem itm in listViewAlert.Items)
            {
                ArrayList row = new ArrayList();
                foreach (ColumnHeader column in listViewAlert.Columns)
                {
                    row.Add(itm.SubItems[column.Index].Text);
                }
                dt.Rows.Add(row.ToArray());
            }

            System.Web.UI.WebControls.DataGrid grid =
                  new System.Web.UI.WebControls.DataGrid();
            grid.HeaderStyle.Font.Bold = true;
            grid.HeaderStyle.Font.Name = "Calibri";
            grid.HeaderStyle.BackColor = Color.Gold;
            grid.Font.Name = "Calibri";
            grid.Font.Size = 9;
            grid.DataSource = ds;
            grid.DataMember = "Table";
            grid.DataBind();
            string filename = "reportSoftmar" + DateTime.Now.Second.ToString() + ".xls";
            string filePath = m_startDefault + filename;
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                using (System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw))
                {
                    grid.RenderControl(hw);
                }
            }
            Link.Open(filename);
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            listViewAlert.Items.Clear();
            statusBar1.Text = "Total : 0 item(s)";
            IList pcMasters = m_profitCentreService.GetAllProfitCentreMapping();
            LoadItems(pcMasters);
        
        }

        private void LoadItems(IList pcMasters)
        {
            foreach (ProfitCentre a in pcMasters)
            {
                ListViewItem item = listViewAlert.Items.Add(a.co_type);
                item.SubItems.Add(a.short_code);
                item.SubItems.Add(a.short_name);
                item.SubItems.Add(a.full_name);
                item.SubItems.Add(a.full_address);
                item.SubItems.Add(a.city);
                item.SubItems.Add(a.country);
                item.SubItems.Add(a.account_code);
                item.SubItems.Add(a.active);
                item.SubItems.Add(a.agr_description);
                item.SubItems.Add(a.agr_dim_value);
                item.SubItems.Add(a.agr_rel_value);
                item.SubItems.Add(a.client);
                item.Tag = a;
            }
            statusBar1.Text = string.Format("Total : {0} item(s)", pcMasters.Count);
        }

        internal void UpdateSelectedItem(ProfitCentre pc)
        {
            if (listViewAlert.SelectedItem == null) return;
            if (listViewAlert.SelectedItem.Tag == null) return;
            listViewAlert.SelectedItem.SubItems[9].Text = pc.agr_description;
            listViewAlert.SelectedItem.SubItems[10].Text = pc.agr_dim_value;
            listViewAlert.SelectedItem.SubItems[11].Text = pc.agr_rel_value;
            listViewAlert.SelectedItem.Tag = pc;
        }

        private void textBoxSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                m_timerSearch.Start();
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void Search()
        {
            listViewAlert.Items.Clear();
            statusBar1.Text = "Total : 0 item(s)";
            IList softmarAlert = m_profitCentreService.GetAllProfitCentreMapping(textBoxSearch.Text);
            LoadItems(softmarAlert);
        }

        private void initTimerSearch()
        {
            m_timerSearch.Interval = 100;
            m_timerSearch.Start();
            m_timerSearch.Tick += new EventHandler(m_timerSearch_Tick);
        }

        void m_timerSearch_Tick(object sender, EventArgs e)
        {
            Search();
            m_timerSearch.Stop();
        }
    }
}